<template>
  <div id="phone">
    <nav-bar>
      <span slot="text"></span>
    </nav-bar>
    <div class="container">
      <input type="text" placeholder="请输入手机号码" v-model="phone" />
      <button @click="uploadPhone">确认修改</button>
    </div>
  </div>
</template>

<script>
import { modifyPhone } from '@/network/getData';
import { mapState, mapMutations } from 'vuex';
export default {
  data() {
    return {
      phone: ''
    };
  },
  computed: {
    ...mapState(['userInfo'])
  },
  methods: {
    ...mapMutations(['MODIFY_PHONE']),
    // 更新用户名称
    uploadPhone() {
      const mobile = /^(13|14|15|18)\d{9}$/;

      if (!mobile.test(this.phone)) {
        console.log('手机号码格式错误');
        return false;
      } else {
        this.showTip = false;
        modifyPhone(this.userInfo.id, this.phone).then(res => {
          console.log(res);
          if (res.code === 200) {
            this.MODIFY_PHONE(this.phone);
            this.$router.go(-1);
          }
        });
      }
    }
  }
};
</script>

<style lang="less" scoped>
#phone {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: #fff;
}
.container {
  margin-top: 45px;
  padding: 10px;
  input {
    display: block;
    width: 100%;
    height: 35px;
    text-indent: 10px;
    border: 1px solid var(--color-border);
    background-color: #eee;
  }
  .active {
    border: 1px solid red;
  }
  span {
    display: block;
    margin-top: 5px;
    font-size: 13px;
    color: red;
  }
  button {
    text-align: center;
    margin-top: 20px;
    width: 100%;
    line-height: 35px;
    border-radius: 5px;
    color: #fff;
    background-color: #4cd964;
  }
}
</style>
